package logic

import (
	"context"
	"encoding/json"
	"fmt"
	"rent-master/models/cache"
	"rent-master/models/mysql"
	"time"

	"rent-master/rent/internal/svc"
	"rent-master/rent/rent"

	"github.com/zeromicro/go-zero/core/logx"
)

type RentListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewRentListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RentListLogic {
	return &RentListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 列表
func (l *RentListLogic) RentList(in *rent.RentListRequest) (*rent.RentListResponse, error) {
	// todo: add your logic here and delete this line

	var Rent mysql.Rent
	var lists []*rent.RentList
	sprintf := fmt.Sprintf("rentList")
	result, _ := cache.Rdb.Get(l.ctx, sprintf).Result()
	if result == "" {
		var err error
		lists, err = Rent.RentList(mysql.DB, lists)
		if err != nil {
			return nil, err
		}
		jsonData, _ := json.Marshal(lists)
		cache.Rdb.Set(l.ctx, sprintf, jsonData, time.Minute*2)
	} else {
		json.Unmarshal([]byte(result), &lists)
	}

	return &rent.RentListResponse{
		List: lists,
	}, nil
}
